// pages/proposal/proposal.js
import {getWorksList,getCategory} from '../../service/config/index.js'
const BACKTOPDIS=500;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true,
    navTab:[], //顶部导航
    showBacktop:false,
    
    listData: [], //数据
    currentNav:0, // 顶部导航索引 默认选中第一列
    pageIndex: 1,
    pageTotal: 1, // 最大页数
    reachBottom: false,
    isFiexd:false,
    tagId:0,
    layuot:2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    //加载tag
    that._getCategory();

    //获取图集
    that._getWorksList();
  },


  /**
   * nav 子组件的回调
   * @param {接收子组件传递的事件} event 
   */
  tabclick(event){
    let that = this
    const index= event.detail.index;
    let item = this.data.navTab[index]
    console.log(item)
    let param = {}
    param.categoryId = item.id==0?null:item.id
    
    getWorksList(param).then(res=>{
      if(res.code ==200){
        that.setData({
          listData:res.rows
        })
      }
    })
    this.setData({
      tagId:index
    })
  },

  /**
   * 瀑布流组件点击后的回调
   * @param {瀑布流组件点击后的回调} event 
   */
  childClick(event){
    console.log(event)
    const index= event.detail.index;
    console.log("点击了",index)
    //跳转页面
    wx.navigateTo({
      url: '/pages/detail/detail?id=' +index,
    })
  },

  search(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  confirm(){
    let that= this
    console.log(this.data.search)
    let param = {}
    _getWorksList(param).then(res=>{
      if(res.code == 0){
        that.setData({
          listData:res.data
        })
      }
    })
  },

  hanldeIconChange(e){
    console.log("切换列表",e.detail)
    this.setData({
      layuot:e.detail
    })
    this._getWorksList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("显示",this.data.search)
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(111)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底")
    this._getWorksList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onPageScroll: function(options){
    const scrollTop = options.scrollTop;
    //回到顶部
    const flag=scrollTop >=BACKTOPDIS
    //官方提示，不建议在onPageScroll 频繁的调用this.setData方法
    //因为会不断的刷新界面
    if(flag!=this.data.showBacktop){
      this.setData({
        showBacktop:flag
      })
    }
 
    //tabcontrol停留效果
    const flag2=scrollTop>=this.data.tabScrollTop;
    if(flag2!=this.data.isFiexd){
      this.setData({
        isFiexd:flag2
      })
    }
  },

  _getWorksList(categoryId){
    // 上拉加载pageIndex += 1
    // 当前页数大于最大页数不执行后续
    let that = this;
    let param = {}
    param.tid = that.data.currentNav
    param.categoryId = categoryId||null
    param.status = 1
    getWorksList(param).then(res=>{
      if(res.code == 200){
        const list = res.rows
        that.fillData(list)
        that.setData({
          listData:list
        })
      }else{
        console.log("获取数据出错")
      }
      // 通知页面停止下拉刷新效果
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 下拉刷新时 isFull 传 true。
   * @param {下拉刷新时} isFull 
   * @param {*} goods 
   */
  fillData: function (data){
    let view = this.selectComponent('#waterFallView');
    if(null != view){
      view.fillData(data);
    }
  },

  /**
   * 获取导航数据
   */
  _getCategory(){
    let that = this;
    let param = {}
    param.type = 2
    param.status = 1
    getCategory(param).then(res=>{
      let nav = res.rows
      nav.unshift({id:0,name:"全部"})
      that.setData({
        navTab:nav,
      })
    })
  },
  

  /**
   * 点击导航触发
   * @param {} e 
   */
  switchTab:function(e){
    let that = this;
    console.log(e)
    that.setData({
      currentNav:e.currentTarget.dataset.index
    })
  },


    // 下拉刷新
    _refresh() {
      this.setData({
        pageIndex: 1,
        pageTotal: 1
      })
      this._getRecommend(false);
    },
})